<?php
class Api_Lib_Rsp_UniApp_Autocomplete {

    function __construct() {
        $this->_config = array(
            'test'     =>'test',
            'purpose'  =>'purposeList',
            'typeList' =>'typeList',
            'gexingList' =>'gexingList',
            'code'     =>'codeList',
            'codeList' =>'getCode',
        );
    }

    /**
     * 页面获取数据的入口方法
     * Time：2019/11/13 08:48:31
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    function getData($params = array(),& $service){
        if(!$params['dataKey']){
            $service->send_user_error('dataKey not found');
        }

        //判断当前的对应方法
        if(!isset($this->_config[$params['dataKey']])){
            $service->send_user_error('dataKey not in config');
        }

        //判断是否配置再其他class中，如果是需要调用其他类
        $pos = strpos($this->_config[$params['dataKey']], '@');
        if($pos > 0){
            list($ctl ,$action) = explode('@', $this->_config[$params['dataKey']]);
            $class = FLEA::getSingleton($ctl);
            $data = $class->$action($params);
        }else{
            $action = $this->_config[$params['dataKey']] ? $this->_config[$params['dataKey']] : $params['dataKey'];
            $data = $this->$action($params);
        }
        //获取页面的配置

        return $data;
    }

    //测试数据
    function test($params){
        $i = mt_rand(0,3);
        if($i==2){
            sleep(3);
        }
        if($params['key']){
            return array('我就是'.$params['key'].':'.$i);
        }
        return array('我是','测试','数据','我是一个很长的测试数据 特别长的那种 再长点 再长点 再长点');
    }

    //样品的类型数据
    function typeList($params){
        $modelType = FLEA::getSingleton('Model_Sample_Ruku');
        $value = $modelType->getTypeValues();
        return $value;
    }

    //样品的类型数据
    function gexingList($params){
        $modelType = FLEA::getSingleton('Model_Sample_Ruku');
        $value = $modelType->getGexingValues();
        return $value;
    }

    //用途数据
    function purposeList($params){
        $modelType = FLEA::getSingleton('Model_Sample_Ruku');
        $value = $modelType->getPurposeValues();
        return $value;
    }

    //条码或者产品编号搜索
    function codeList($params){
        $modelRuku2Product = FLEA::getSingleton('Model_Sample_Ruku2Product');
        
        $value = $modelRuku2Product->getCode();
        return $value;
    }

    function getCode($params){
        $_modelExample = FLEA::getSingleton('Model_Sample_Ruku2Product');
        $_modelProduct = FLEA::getSingleton('Model_Jichu_Product');
        $baseUrlstr = detect_uri_base();
        $serverUrl = substr($baseUrlstr, 0,strrpos(strtolower($baseUrlstr), 'api.php'));

        $sql = "SELECT x.* FROM sample_ruku2product x
            left join jichu_product y on y.id = x.productId 
            where substr(y.prefix, -5) REGEXP '^[0-9]*$'";
        if($params['key']){
            $sql .= " and x.code like '%{$params['key']}%'";
        }
        $sql .= " group by x.code order by substr(y.prefix, -5) desc limit 0,5";
        $row = $_modelExample->findBySql($sql);
        $arr = array();
        if(count($row) > 0){
            foreach($row as & $v){
              $arr[] = $v['code'];
            }
        }else{
            $condition[] = array('proCode',"%{$params['key']}%",'like');
            if($_GET['kindId']){
                $condition[] = array('kindId',$_GET['kindId'],'=');
            }
            $condition[] = array('isSample',1,'=');
            
            $row = $_modelProduct->findAll($condition, 'proCode' ,20);
            foreach($row as & $v){
              $arr[] = $v['proCode'];
            }
        }
        return $arr;
    }
}
?>